package com.ziyear.common.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.log4j.Logger;

import java.io.FileReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;


/**
 * 功能描述 : JDBCUtil
 *
 * @author Ziyear
 */
public class JDBCUtil {

    private static String driver;
    private static String url;
    private static String user;
    private static String password;

    private static DruidDataSource dataSource = null;

    private static Logger logger = Logger.getLogger(JDBCUtil.class.getName());

    static {
        //读取文件，获取值
        try {
            //1.创建Properties集合类
            Properties pro = new Properties();
            //获取src路径下的文件--->ClassLoader类加载器
            ClassLoader classLoader = JDBCUtil.class.getClassLoader();
            URL resource = classLoader.getResource("jdbc.properties");
            assert resource != null;
            String path = resource.getPath();
            //2.加载文件
            pro.load(new FileReader(path));
            //3获取数据
            url = pro.getProperty("jdbc.url");
            user = pro.getProperty("jdbc.user");
            password = pro.getProperty("jdbc.password");
            driver = pro.getProperty("jdbc.driver");
            //4.配置数据源
            dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(user);
            dataSource.setPassword(password);
            dataSource.setInitialSize(10);
            dataSource.setMaxActive(50);
            dataSource.setMinIdle(1);
        } catch (Exception e) {
            logger.error("注册Driver驱动发生异常。", e);
            e.printStackTrace();
        }
    }

    public static void close(Connection conn) throws SQLException {
        if (threadLocal.get() != null) {
            return;
        }
        conn.close();
    }

    /**
     * 返回数据库链接 事务控制
     *
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            if (threadLocal.get() != null) {
                return threadLocal.get();
            }
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            logger.error("获取Connection连接发生异常。", e);
            e.printStackTrace();
        }
        return conn;
    }

    private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<>();


    public static void startTransaction() {
        try {
            Connection connection = threadLocal.get();
            if (connection == null) {
                connection = getConnection();
                connection.setAutoCommit(false);
                threadLocal.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void commit() {
        try {
            Connection connection = threadLocal.get();
            if (connection != null) {
                connection = getConnection();
                connection.commit();
                connection.close();
                threadLocal.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void rollback() {
        try {
            Connection connection = threadLocal.get();
            if (connection != null) {
                connection = getConnection();
                connection.rollback();
                connection.close();
                threadLocal.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
	
